package cn.wl.hrm.service.impl;

import cn.wl.hrm.doc.CourseDoc;
import cn.wl.hrm.domain.Course;
import cn.wl.hrm.domain.CourseDetail;
import cn.wl.hrm.domain.CourseMarket;
import cn.wl.hrm.dto.CourseAddDto;
import cn.wl.hrm.feign.client.ESFeignClient;
import cn.wl.hrm.mapper.CourseDetailMapper;
import cn.wl.hrm.mapper.CourseMapper;
import cn.wl.hrm.mapper.CourseMarketMapper;
import cn.wl.hrm.query.CourseQuery;
import cn.wl.hrm.service.ICourseService;
import cn.wl.hrm.util.AjaxResult;
import cn.wl.hrm.util.PageList;
import cn.wl.hrm.util.UserContext;
import cn.wl.hrm.vo.LoginInfo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wl
 * @since 2020-04-21
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseDetailMapper courseDetailMapper;

    @Autowired
    private CourseMarketMapper courseMarketMapper;

    @Autowired
    private ESFeignClient esFeignClient;

    @Override
    public void saveOrUpdate(CourseAddDto courseAddDto) {
        //获取到Course对象
        Course course = courseAddDto.getCourse();
        //获取到CourseDetail对象
        CourseDetail courseDetail = courseAddDto.getCourseDetail();
        //获取到CourseMarket对象
        CourseMarket courseMarket = courseAddDto.getCourseMarket();

        //首先对参数进行判断
        if(!StringUtils.hasLength(course.getName())){
            throw new RuntimeException("课程名不能为空！");
        }
        //1、将课程保存到Course表中
        //设置状态
        course.setStatus(Course.STATE_DOWN);
        //拿到登录信息，设置到课程对象里
        LoginInfo loginInfo = UserContext.getLoginInfo();
        course.setUserId(loginInfo.getUserId());
        course.setUserName(loginInfo.getUserName());
        course.setTenantId(loginInfo.getTenantId());
        course.setTenantName(loginInfo.getTenantName());
        //这里的添加只有在用户登录之后才能进行，所以不需要new对象进行重新赋值，直接添加就行
        baseMapper.insert(course);

        //2、将课程详情相关的内容保存到CourseDetail表中
        //courseDetail的ID就是course的ID，它们是一对一的关系
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);

        //3、将课程营销相关的内容保存到CourseMarket表中
        //courseDetail的ID就是course的ID，它们是一对一的关系
        courseMarket.setId(course.getId());
        //设置有效性
        courseMarket.setValid(CourseMarket.STATE_VALID);
        courseMarketMapper.insert(courseMarket);
    }

    /**
     * 课程上线
     */
    @Override
    @Transactional
    public void onLineCourse(Long id) {
        //根据Id查询到course对象
        Course course = baseMapper.selectById(id);
        //判断course是否存在
        if(course == null){
            throw new RuntimeException("课程不能为空");
        }
        //判断课程是否是下线状态
        if(course.getStatus() == Course.STATE_DOWN){
            //修改课程状态为上线
            course.setStatus(Course.STATE_UP);
            Integer integer = baseMapper.updateById(course);
            if(integer == 1){
                //将课程添加到ES中去
                CourseDoc doc = new CourseDoc();
                //获取到课程营销
                CourseMarket courseMarket = courseMarketMapper.selectById(id);
                //获取到课程详情
                CourseDetail courseDetail = courseDetailMapper.selectById(id);
                //使用BeanUtils工具类进行属性的复制
                BeanUtils.copyProperties(courseMarket,doc);
                BeanUtils.copyProperties(courseDetail,doc);
                BeanUtils.copyProperties(course,doc);
                //设置上线时间
                doc.setOnlineTime(new Date());
                //将查询出来的charge转换为Integer类型
                doc.setCharge(courseMarket.getCharge().intValue());
                AjaxResult ajaxResult = esFeignClient.onLineCourse(doc);
                if(!ajaxResult.isSuccess()){
                    throw new RuntimeException("课程状态修改失败");
                }
            }else {
                throw new RuntimeException("课程状态修改失败");
            }
        }else {
            throw new RuntimeException("无效的课程状态");
        }
    }

    /**
     * 课程下线
     */
    @Override
    @Transactional
    public void offLineCourse(Long id) {
        //根据Id查询到course对象
        Course course = baseMapper.selectById(id);
        //判断course是否存在
        if(course == null){
            throw new RuntimeException("课程不能为空");
        }
        //判断课程是否是上线状态
        if(course.getStatus() == Course.STATE_UP){
            //修改课程状态为下线
            course.setStatus(Course.STATE_DOWN);
            Integer integer = baseMapper.updateById(course);
            if(integer == 1){
                //将课程从ES中删除
                AjaxResult ajaxResult = esFeignClient.offLineCourse(id);
                if(!ajaxResult.isSuccess()){
                    throw new RuntimeException("课程状态修改失败");
                }
            }else {
                throw new RuntimeException("课程状态修改失败");
            }
        }else {
            throw new RuntimeException("无效的课程状态");
        }
    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        return esFeignClient.searchCourse(courseQuery);
    }
}
